package com.source.lightfowl.barcodeMachineGrantAndRecovery.service.impl;

import com.source.lightfowl.VO.BarcodeMachineRecoveryVO;
import com.source.lightfowl.barcodeMachineGrantAndRecovery.dao.BarcodeMachineRecoveryVersion2Dao;
import com.source.lightfowl.barcodeMachineGrantAndRecovery.pojo.BarcodeMachineRecovery;
import com.source.lightfowl.barcodeMachineGrantAndRecovery.service.BarcodeMachineRecoveryService;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.utils.StringTOArrayList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: xcy
 * @Date:Create：in 2020/3/16 22:12
 * @Description: 脚标机回收管理 业务层实现类
 */
@Service
public class BarcodeMachineRecoveryServiceImpl implements BarcodeMachineRecoveryService {

    //注入持久层接口实现类
    @Autowired
    private BarcodeMachineRecoveryVersion2Dao barcodeMachineRecoveryDao;
    @Autowired
    private PersonDao personDao;
    /*
    查找全部
     */
    @Override
    public List<BarcodeMachineRecoveryVO> findAll() {
        List<BarcodeMachineRecovery> list = barcodeMachineRecoveryDao.findAll();

        List<BarcodeMachineRecoveryVO> collect = list.stream().map(s -> {
            BarcodeMachineRecoveryVO vo = new BarcodeMachineRecoveryVO();
            BeanUtils.copyProperties(s,vo);

            //查找回收人资料
            Person user = personDao.findVO(s.getRecoveryPersonId());
            if (user != null) {
                vo.setRecyclingName(user.getLoginName());
            }

            //查找发放人资料
            Person user1 = personDao.findVO(s.getSendBackPersonId());
            if (user1 != null) {
                vo.setSendName(user1.getLoginName());
            }

            Person user3 = personDao.findVO(s.getCreateBy());
            if (user3 != null) {
                vo.setCreateName(user3.getLoginName());
            }

            return vo;
        }).collect(Collectors.toList());


        return collect;
    }

    /*
    分页查询
     */
    @Override
    public Page<BarcodeMachineRecovery> findAllPage(int page, int pageNum) {

        return barcodeMachineRecoveryDao.findAll(PageRequest.of(page-1,pageNum));
    }

    /*
    根据脚标机的id查询
     */
    @Override
    public List<BarcodeMachineRecovery> findByBarcodeMachineId(Integer id) {
        return barcodeMachineRecoveryDao.findByBarcodeMachineId(id);
    }

    /*
    根据id查询
     */
    @Override
    public BarcodeMachineRecoveryVO findById(Integer id) {
        Optional<BarcodeMachineRecovery> optional = barcodeMachineRecoveryDao.findById(id);
        if (optional.isPresent()){
            BarcodeMachineRecovery s = optional.get();
            BarcodeMachineRecoveryVO vo = new BarcodeMachineRecoveryVO();
            BeanUtils.copyProperties(s,vo);
            Person user = personDao.findVO(s.getRecoveryPersonId());
            //发放人姓名
            if (user != null) {
                vo.setRecyclingName(user.getLoginName());
            }

            Person user1 = personDao.findVO(s.getSendBackPersonId());
            //发放人姓名
            if (user1 != null) {
                vo.setSendName(user1.getLoginName());
            }

            Person user3 = personDao.findVO(s.getCreateBy());
            if (user3 != null) {
                vo.setCreateName(user3.getLoginName());
            }
            return vo;

        }

        return null;
    }

    /*
    增加/修改
     */
    @Override
    public void save(BarcodeMachineRecovery barcodeMachineRecovery) {
        //更新创建时间
        barcodeMachineRecovery.setCreateTime(LocalDateTime.now());
        barcodeMachineRecoveryDao.save(barcodeMachineRecovery);
    }

    /*
    根据id删除
     */
    @Override
    public void deleteById(Integer id) {
        barcodeMachineRecoveryDao.deleteById(id);
    }

    /*
    批量删除
     */
    @Override
    public void deleteBatch(String ids) {
        ArrayList<Integer> list = StringTOArrayList.toArrayList(ids);
        if(list != null) {
            barcodeMachineRecoveryDao.deleteBatch(list);
        }else {
            System.out.println("批量删除传递参数出错");
            throw new RuntimeException("传递的id参数格式出错，id之间需要用英文逗号分隔开，以字符串格式传递");
        }
    }
}